﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Diagnostics;

namespace FireControlSystem
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public class DelegateCollection
        {
            public delegate void updateUIContorl(string txt);
        }
        /// <summary>
        /// 计算程序运行时间
        /// </summary>
        /// <param name="ontime">输入启动时间</param>
        /// <returns>返回格式化运行时间</returns>
        public string GetRunningTime(string ontime)
        {
            DateTime on = Convert.ToDateTime(ontime);
            TimeSpan rt = DateTime.Now - on;

            string rts = Math.Floor(rt.TotalHours).ToString("00") + ":" + Math.Floor(rt.TotalMinutes).ToString("00") + ":" + Math.Floor(rt.TotalSeconds).ToString("00");
            return rts;
        }
        private  void DataControl(string st,int timegap)
        {
            try { 
                while (true) {
                    //实际时间
                    String dt = DateTime.Now.ToString("HH:mm:ss");
                    //设定NowTime组件
                    this.NowTime.Dispatcher.Invoke(new Action(delegate
                    {
                        this.NowTime.Content = dt;
                    }));

                    //运行时间
                    String rt = GetRunningTime(st);
                    //设定RunningTime组件
                    this.RunningTime.Dispatcher.Invoke(new Action(delegate
                    {
                        this.RunningTime.Content = rt;
                    }));
           
                    //预警等级
                    Random ran = new Random();
                    int  alert=ran.Next(0, 10);
                    this.AlertLevel.Dispatcher.Invoke(new Action(delegate
                    {
                        this.AlertLevel.Value = alert;
                        this.AlertLevel.Text = "预警等级" + alert.ToString();
                    }));


                    //供电量
                    double com1 = 0.5;//单次耗电量
                    this.AlertLevel.Dispatcher.Invoke(new Action(delegate
                    {
                        if(this.MainBattery.Value- com1 >= 0)
                        {
                            this.MainBattery.Value -= com1;
                        }
                        else
                        {
                            this.MainBattery.Value = 0;
                        }
                    }));

                    /**********************仓储库房*************************/
                    //即时温度
                    double temperNow1 = ran.Next(20, 30) + ran.NextDouble();
                    this.temper1.Dispatcher.Invoke(new Action(delegate
                    {
                        this.temper1.Value = temperNow1;
                    }));
                    //低温区温度
                    double temperLow1 = ran.Next(-3, 3) + ran.NextDouble();
                    this.temperLow1.Dispatcher.Invoke(new Action(delegate
                    {
                        this.temperLow1.Value = temperLow1;
                    }));
                    //灭火剂存量
                    double com2 = 1;
                    this.extSto.Dispatcher.Invoke(new Action(delegate
                    {
                        if (this.extSto.Value - com2 >= 0)
                        {
                            this.extSto.Value -= com2;
                        }
                        else
                        {
                            this.extSto.Value = 0;
                        }
                    }));
                    //相对湿度
                    double humi = ran.Next(10, 20) + ran.NextDouble();
                    this.humi.Dispatcher.Invoke(new Action(delegate
                    {
                        this.humi.Value = humi;
                    }));
                    //负载平衡
                    double balance = ran.Next(10, 20) + ran.NextDouble();
                    this.balance.Dispatcher.Invoke(new Action(delegate
                    {
                        this.balance.Value = balance;
                    }));

                    /**********************机房*************************/
                    //即时温度
                    double temperNow2 = ran.Next(30, 40) + ran.NextDouble();
                    this.temper2.Dispatcher.Invoke(new Action(delegate
                    {
                        this.temper2.Value = temperNow2;
                    }));
                    //平均载荷
                    double avLoad = ran.Next(40, 50) + ran.NextDouble();
                    this.avLoad.Dispatcher.Invoke(new Action(delegate
                    {
                        this.avLoad.Value = avLoad;
                    }));
                    //机柜温度
                    double temperCase = ran.Next(70, 80) + ran.NextDouble();
                    this.temperCase.Dispatcher.Invoke(new Action(delegate
                    {
                        this.temperCase.Value = temperCase;
                    }));
                    //CPU组温度
                    double temperCPU = ran.Next(80, 90) + ran.NextDouble();
                    this.temperCPU.Dispatcher.Invoke(new Action(delegate
                    {
                        this.temperCPU.Value = temperCPU;
                    }));
                    //GPU组温度
                    double temperGPU = ran.Next(20, 30) + ran.NextDouble();
                    this.temperGPU.Dispatcher.Invoke(new Action(delegate
                    {
                        this.temperGPU.Value = temperGPU;
                    }));
                    //总电源开关
                    this.mainSwitch.Dispatcher.Invoke(new Action(delegate
                    {
                        this.mainSwitch.TextOn = "开启";
                        this.mainSwitch.TextOff = "关闭";
                    }));
                    //CPU组开关
                    this.cpu.Dispatcher.Invoke(new Action(delegate
                    {
                        this.cpu.TextOn = "开启";
                        this.cpu.TextOff = "关闭";
                    }));
                    //GPU开关
                    this.gpu.Dispatcher.Invoke(new Action(delegate
                    {
                        this.gpu.TextOn = "开启";
                        this.gpu.TextOff = "关闭";
                    }));
                    //风扇开关
                    this.fan.Dispatcher.Invoke(new Action(delegate
                    {
                        this.fan.TextOn = "开启";
                        this.fan.TextOff = "关闭";
                    }));




                    /**********************总控室*************************/
                    //即时温度
                    double temperNow3 = ran.Next(30, 40) + ran.NextDouble();
                    this.temper3.Dispatcher.Invoke(new Action(delegate
                    {
                        this.temper3.Value = temperNow3;
                    }));
                    //电压
                    double voltage = ran.Next(20, 50) + ran.NextDouble();
                    this.voltage.Dispatcher.Invoke(new Action(delegate
                    {
                        this.voltage.Value = voltage;
                    }));
                    //消防用水
                    double com3 = 5;
                    this.water.Dispatcher.Invoke(new Action(delegate
                    {
                        if (this.water.Value - com3 >= 0)
                        {
                            this.water.Value -= com3;
                        }
                        else
                        {
                            this.water.Value = 0;
                        }
                    }));
                    //燃料
                    double com4 = 5;
                    this.fuel.Dispatcher.Invoke(new Action(delegate
                    {
                        if (this.fuel.Value - com4 >= 0)
                        {
                            this.fuel.Value -= com4;
                        }
                        else
                        {
                            this.fuel.Value = 0;
                        }
                    }));
                    //测点温度
                    double temp1_1 = ran.Next(20, 22) + ran.NextDouble();
                    double temp1_2 = ran.Next(20, 22) + ran.NextDouble();
                    double temp2_1 = ran.Next(48, 50) + ran.NextDouble();
                    double temp2_2 = ran.Next(48, 50) + ran.NextDouble();
                    this._1_1.Dispatcher.Invoke(new Action(delegate
                    {
                        this._1_1.Value = temp1_1;
                    }));
                    this._1_2.Dispatcher.Invoke(new Action(delegate
                    {
                        this._1_2.Value = temp1_2;
                    }));
                    this._2_1.Dispatcher.Invoke(new Action(delegate
                    {
                        this._2_1.Value = temp2_1;
                    }));
                    this._2_2.Dispatcher.Invoke(new Action(delegate
                    {
                        this._2_2.Value = temp2_2;
                    }));
                    //转机转速
                    double rotateSpeed = ran.Next(2200, 3400) + ran.NextDouble();
                    this.motor.Dispatcher.Invoke(new Action(delegate
                    {
                        this.motor.Value = rotateSpeed;
                    }));
                    //均气压
                    double avPress = ran.Next(1000, 1200) + ran.NextDouble();
                    this.avPress.Dispatcher.Invoke(new Action(delegate
                    {
                        this.avPress.Value = avPress;
                    }));

                    Thread.Sleep(timegap);
                }
            }
            catch(TaskCanceledException e)
            {
                
            }
        }
        public MainWindow()
        {
            InitializeComponent();
            //启动时间
            string st = DateTime.Now.ToLongTimeString().ToString();
            //线程刷新时间(ms)
            const int timegap = 500;
            //执行数据模拟
            Thread thread = new Thread(new ThreadStart(new Action(() =>
                {
                    DataControl(st,timegap);
                })));
            thread.Start();
            }        
    }
}
